package cn.findviewbydream.evil.main.controller;

import cn.findviewbydream.evil.main.dto.PostListBean;
import cn.findviewbydream.evil.main.service.CodeService;
import cn.findviewbydream.evil.main.utils.Utils;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/********************************************
 * 工程名：HebeuCS
 * 包名：cn.findviewbydream.evil.main.controller
 * 时间：16-11-4 下午9:09
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：代码讨论区(主页)
 ********************************************/
@Controller
public class CodeController
{
    private final CodeService codeService;

    @Autowired
    public CodeController(CodeService codeService)
    {
        this.codeService = codeService;
    }

    /**
     * 入口
     *
     * @return jsp
     */
    @RequestMapping("/code/index")
    public String code(Model model)
    {
        model.addAttribute("codetypeList", codeService.getCodeTypeList());
        return "index_code";
    }

    /**
     * 通过ajax.load加载列表内容
     *
     * @param model   model
     * @param session session.user判断此用户是否点赞
     * @param type    代码类型
     * @param cpn     想要获得的页数
     * @return jsp
     */
    @RequestMapping("/code/get/postlist/{type}/{cpn}")
    public String postList(Model model, HttpSession session,
                           @PathVariable("type") String type,
                           @PathVariable("cpn") int cpn)
    {
        model.addAttribute("postListBean", codeService.getPostList(session, type, cpn));
        Utils.sleep(300);
        return "code/post_list";
    }
}
